//
//  YBAlreadySignUpMeetingViewController.m
//  GongXinDangJian
//
//  Created by apple on 17/12/7.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "YBAlreadySignUpMeetingViewController.h"
#import "LPMeetingActModel.h"
#import "LPMeetingActTableViewCell.h"
#define CELLID @"YBAlreadySignUpMeetingViewController"
#import "YBMeetingDetailViewController.h"
@interface YBAlreadySignUpMeetingViewController ()<UITableViewDelegate,UITableViewDataSource,LPMeetingActTableViewCellDelegate>
/**  tablvie  */
@property (strong, nonatomic) UITableView  *tableView;
/**  未报名数据  */
@property (strong, nonatomic) NSMutableArray  *dataSouce;
/**  已过期数据数据  */
@property (strong, nonatomic) NSMutableArray  *pastDataSouce;

/**  页数  */
@property (assign, nonatomic) NSInteger  page;
/** 滑动 */
@property (nonatomic, strong) ZJRefurbish *refurbish;
/**  是否报名  */
@property (strong, nonatomic) NSString  *IS_ENROLL;
@end

@implementation YBAlreadySignUpMeetingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.IS_ENROLL = @"1";
    __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
        weakSelf.page = 1;
        [weakSelf netWorking_header];
        
    }];
    [self.refurbish footerToScollview:self.tableView Block:^{
        weakSelf.page ++;
        [weakSelf netWorking_footer];
    }];
    [self layoutUI];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView.mj_header beginRefreshing];
}


#pragma mark - 数据
//下拉刷新
- (void)netWorking_header{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat:@"%ld",self.page];
    [LPRequestManager request_miitactionHyActiongetShListWithIS_ENROLL:self.IS_ENROLL PAGE_INDEX:page   successBlock:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE integerValue] == 20000) {
            /**  未报名数据  */
            NSArray *array = [object objectForKey:@"W_HYS"];
            weakSelf.dataSouce = [[NSArray yy_modelArrayWithClass:[LPMeetingActModel class] json:array] mutableCopy];
            /**  历史数据  */
            NSArray *past = [object objectForKey:@"W_HYS_ED"];
            weakSelf.pastDataSouce = [[NSArray yy_modelArrayWithClass:[LPMeetingActModel class] json:past] mutableCopy];
            
            [weakSelf.tableView reloadData];
        }else{
            [YJProgressHUD showSuccess:@"数据加载失败" inview:[UIApplication sharedApplication].keyWindow];
        }
        [weakSelf.tableView.mj_header endRefreshing];
    } failBlock:^(id object) {
        [weakSelf.tableView.mj_header endRefreshing];
        [YJProgressHUD showSuccess:@"网络错误" inview:[UIApplication sharedApplication].keyWindow];
    }];
    
    
    
    
    
}

//上拉加载
- (void)netWorking_footer{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat:@"%ld",self.page];
    [LPRequestManager request_miitactionHyActiongetShListWithIS_ENROLL:self.IS_ENROLL PAGE_INDEX:page   successBlock:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE integerValue] == 20000) {
            /**  未报名数据  */
            NSArray *array = [object objectForKey:@"W_HYS"];
            
            /**  历史数据  */
            NSArray *past = [object objectForKey:@"W_HYS_ED"];
            
            
            if (array.count == 0 && past.count == 0) {
                _page --;
            }else{
                
                
                [ weakSelf.dataSouce addObjectsFromArray:[NSArray yy_modelArrayWithClass:[LPMeetingActModel class] json:array]];
                
                [weakSelf.pastDataSouce addObjectsFromArray: [NSArray yy_modelArrayWithClass:[LPMeetingActModel class] json:past]];
                [weakSelf.tableView reloadData];
            }
            
        }
        [weakSelf.tableView.mj_footer endRefreshing];
    } failBlock:^(id object) {
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
    
    
    
}


#pragma mark - 界面
- (void)layoutUI{
    [self.view addSubview:self.tableView];
    TenGrayShadowView *ten = [[TenGrayShadowView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 10 * [UIScreen zj_screenHScale])];
    self.tableView.tableHeaderView = ten;
    
}


#pragma mark - LPMeetingActTableViewCellDelegate

- (void)LPMeetingActTableViewCellDeleteWithModel:(LPMeetingActModel *)model row:(NSInteger)row{
    [YJProgressHUD showMessage:@"删除中..." duration:10 view:[UIApplication sharedApplication].keyWindow];
    /**  删除  */
    [LPRequestManager request_miitactionHyActiondelHisHyWithHY_ID:model.ID successBlock:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE integerValue] == 20000) {
            [YJProgressHUD showSuccess:@"删除成功" inview:[UIApplication sharedApplication].keyWindow];
            [self.pastDataSouce removeObjectAtIndex:row];
            [self.tableView reloadData];
        }else{
            [YJProgressHUD showSuccess:@"删除失败" inview:[UIApplication sharedApplication].keyWindow];
        }
    } failBlock:^(id object) {
        [YJProgressHUD showSuccess:@"删除失败" inview:[UIApplication sharedApplication].keyWindow];
    }];
}


#pragma mark - UITableViewDataSource,UITableViewDelegate
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (self.pastDataSouce.count != 0 && self.dataSouce.count != 0) {
        return 2;
    }else{
        return 1;
    }
    
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        if (self.dataSouce.count == 0 && self.pastDataSouce.count != 0) {
            return self.pastDataSouce.count;
        } else {
            return self.dataSouce.count;
        }
        
    }else{
        return self.pastDataSouce.count;
    }
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (self.dataSouce.count == 0 && self.pastDataSouce.count != 0) {
            LPMeetingActModel *model = self.pastDataSouce[indexPath.row];
            if (indexPath.row != (self.pastDataSouce.count - 1)){
                return model.CELLHEIGHT - 70 * [UIScreen zj_screenHScale];
            }
            else{
                return model.CELLHEIGHT - 80 * [UIScreen zj_screenHScale];
            }
            
        } else {
            LPMeetingActModel *model = self.dataSouce[indexPath.row];
            if (indexPath.row != (self.dataSouce.count - 1)){
                return model.CELLHEIGHT - 70 * [UIScreen zj_screenHScale];
            }
            else{
                return model.CELLHEIGHT - 80 * [UIScreen zj_screenHScale];
            }
        }
        
    }else{
        LPMeetingActModel *model = self.pastDataSouce[indexPath.row];
        if (indexPath.row != (self.pastDataSouce.count - 1)){
            return model.CELLHEIGHT - 70 * [UIScreen zj_screenHScale];
        }
        else{
            return model.CELLHEIGHT - 80 * [UIScreen zj_screenHScale];
        }
    }
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LPMeetingActTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELLID forIndexPath:indexPath];
    cell.delegate = self;
    cell.row = indexPath.row;
    cell.IS_ENROLL = self.IS_ENROLL;
    if (indexPath.section == 0) {
        if (self.dataSouce.count == 0 && self.pastDataSouce.count != 0) {
            LPMeetingActModel *model = self.pastDataSouce[indexPath.row];
            if (indexPath.row != (self.pastDataSouce.count - 1) && [self.IS_ENROLL boolValue]) {
                cell.bottomView.hidden = NO;
            }
            else{
                cell.bottomView.hidden = YES;
            }
            cell.model = model;
        } else {
            LPMeetingActModel *model = self.dataSouce[indexPath.row];
            if (indexPath.row != (self.dataSouce.count - 1) && [self.IS_ENROLL boolValue]) {
                cell.bottomView.hidden = NO;
            }
            else{
                cell.bottomView.hidden = YES;
            }
            cell.model = model;
        }
    }else{
        LPMeetingActModel *model = self.pastDataSouce[indexPath.row];
        if (indexPath.row != (self.pastDataSouce.count - 1) && [self.IS_ENROLL boolValue]) {
            cell.bottomView.hidden = NO;
        }
        else{
            cell.bottomView.hidden = YES;
        }
        cell.model = model;
    }
    
    return cell;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section == 1) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 40 * [UIScreen zj_screenHScale])];
        label.text = @"历史消息";
        label.textColor = [UIColor lightGrayColor];
        label.backgroundColor = [UIColor themeGrayColor];
        label.font = LYPFONT(17);
        label.textAlignment = NSTextAlignmentCenter;
        return label;
    }else{
        if (self.dataSouce.count == 0 && self.pastDataSouce.count != 0) {
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 40 * [UIScreen zj_screenHScale])];
            label.text = @"历史消息";
            label.textColor = [UIColor lightGrayColor];
            label.backgroundColor = [UIColor themeGrayColor];
            label.font = LYPFONT(17);
            label.textAlignment = NSTextAlignmentCenter;
            return label;
        } else {
            return nil;
        }
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        
        return 40 * [UIScreen zj_screenHScale];
    }else{
        if (self.dataSouce.count == 0 && self.pastDataSouce.count != 0) {
            return 40 * [UIScreen zj_screenHScale];
        } else {
            return 0;
        }
        
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 1) {
        return 0.000000001;
    } else {
        return 0.000000001;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
/**  cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    LPMeetingActModel *model;
    if (indexPath.section == 0) {
        if (self.dataSouce.count == 0 && self.pastDataSouce.count != 0) {
            model = self.pastDataSouce[indexPath.row];
            YBMeetingDetailViewController * vc = [[YBMeetingDetailViewController alloc]init];
            YBMeetingReleaseModel * tempModel = [[YBMeetingReleaseModel alloc]init];
            tempModel.ID = model.ID;
            tempModel.TITLE = model.TITLE;
            vc.model = tempModel;
            vc.isSignIn = NO;
            vc.isShowButton = YES;
            [self.navigationController pushViewController:vc animated:YES];
            
        } else {
            model = self.dataSouce[indexPath.row];
            YBMeetingDetailViewController * vc = [[YBMeetingDetailViewController alloc]init];
            YBMeetingReleaseModel * tempModel = [[YBMeetingReleaseModel alloc]init];
            tempModel.ID = model.ID;
            tempModel.TITLE = model.TITLE;
            vc.model = tempModel;
            vc.isSignIn = NO;
            vc.isShowButton = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }else{
        model = self.pastDataSouce[indexPath.row];
        YBMeetingDetailViewController * vc = [[YBMeetingDetailViewController alloc]init];
        YBMeetingReleaseModel * tempModel = [[YBMeetingReleaseModel alloc]init];
        tempModel.ID = model.ID;
        tempModel.TITLE = model.TITLE;
        vc.model = tempModel;
        vc.isSignIn = NO;
        vc.isShowButton = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}

#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - TabbarHeight- navHeight - 50 * [UIScreen zj_screenHScale]) style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorColor = [UIColor themeGrayColor];
        _tableView.tableFooterView = [UIView new];
        [_tableView registerClass:[LPMeetingActTableViewCell class] forCellReuseIdentifier:CELLID];
    }
    return _tableView;
}


- (NSMutableArray *)dataSouce{
    if (!_dataSouce) {
        _dataSouce = [NSMutableArray array];
    }
    return _dataSouce;
}

- (NSMutableArray *)pastDataSouce{
    if (_pastDataSouce == nil) {
        _pastDataSouce = [NSMutableArray array];
    }
    return _pastDataSouce;
}

- (ZJRefurbish *)refurbish{
    if (!_refurbish) {
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}

@end
